package com.phynos.web.manager.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaseService {

public static final String REG_NUMBER = "[0-9]+";
	
	public static final String REG_CHAR_NUMBER = "[0-9a-zA-Z]+";
	/**
	 * 密码
	 */
	public static final String REG_CHAR_PASSWORD = "[0-9a-zA-Z_]{6,20}";
	/**
	 * 密码，允许为空字符
	 */
	public static final String REG_CHAR_PASSWORD_ALLOW_EMPTY = "^$|^[0-9a-zA-Z_]{6,20}$";
	
	public static boolean checkReg(String val,String regEx){
		return checkReg(val,regEx,false);
	}
	
	/**
	 * 
	 * @param val
	 * @param regEx
	 * @param allowNull 是否允许为空
	 * @return
	 */
	public static boolean checkReg(String val,String regEx,boolean allowNull){
		if(!allowNull && val == null)
			return false;
		else if(allowNull && val == null)
			return true;
		// 编译正则表达式
	    Pattern pattern = Pattern.compile(regEx);
	    Matcher matcher = pattern.matcher(val);
	    // 字符串是否与正则表达式相匹配
	    boolean rs = matcher.matches();
		return rs;
	}
	
	public static long parseToLong(Object val,long defaultVal){
		long result = defaultVal;
		try {
			result = Long.parseLong(val.toString());	
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return result;
	}
	
	private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static String formatDatetime(Date date){
		String result = "";		
		try {
			result = SDF.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
